﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using jSLCore.Client.Presentation.jBinding;

namespace jSLApp.Client.Controls.SoundPlayer
{
    public partial class EasySlider : UserControl
    {
        public EasySlider()
        {
            InitializeComponent();
        }

        public double Value
        {
            get { return GetPositionWidth()/ActualWidth; }
            set
            {
                positionRect.Width = ActualWidth*value;
            }
        }

        private double GetPositionWidth()
        {
            return positionRect.ActualWidth-4/*Padding*/;
        }

        #region Customizing
        public CornerRadius CornerRadius
        {
            get { return border.CornerRadius; }
            set { border.CornerRadius = value; }
        }

        public new Thickness BorderThickness
        {
            get { return border.BorderThickness; }
            set { border.BorderThickness = value; }
        }

        public new Brush BorderBrush
        {
            get { return border.BorderBrush; }
            set { border.BorderBrush = value; }
        }

        public new Brush Background
        {
            get { return border.Background; }
            set { border.Background = value; }
        }

        public Brush Fill
        {
            get { return positionRect.Fill; }
            set { positionRect.Fill = value; }
        }

        public Brush Stroke
        {
            get { return positionRect.Stroke; }
            set { positionRect.Stroke = value; }
        }
        

        public double StrokeThickness
        {
            get { return positionRect.StrokeThickness; }
            set { positionRect.StrokeThickness = value; }
        } 
        #endregion

        private void mouse_Down(object sender, MouseButtonEventArgs e)
        {
            var position = e.GetPosition(this);

            positionRect.Width = position.X+4;

            InvokeSeek(position.X/ActualWidth);
        }

        public event EventHandler<GenericEventArgs<double>> Seek;
        public void InvokeSeek(double value)
        {
            EventHandler<GenericEventArgs<double>> handler = Seek;
            if (handler != null) handler(this, new GenericEventArgs<double>(value));
        }

        public string Text
        {
            get { return txt.Text; }
            set { txt.Text = value; }
        }
    }
}
